<template>
  <div class="newgood">
    <div class="dialog animated" :class="open? 'fadeIn':'fadeOut'" v-show="show" :style="{visibility: (visible?'visible':'hidden')}">
      <div class="panel animated" :class="open? 'zoomIn':''">
        <div class="title">
          {{title}}
          <div class="close" @click="cancel()"></div>
        </div>
        <div>
          <el-form label-width="140px">
            <el-form-item label="商品名称" :rules="[
      { required: true, message: ''}]">
              <el-input ref="name" :disabled="!isAdd" style="width:50%" clearable v-model="goodData.name" placeholder="请输入商品名称"></el-input>
            </el-form-item>
            <el-form-item label="商品类型" :rules="[
      { required: true, message: ''}]">
              <el-select ref="goodsMode" style="width:50%" v-model="goodData.goodsMode" placeholder="请选择商品类型">
                <el-option v-for="item in goodsModeList" :key="item.value" :label="item.name" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="成本价">
              <span style="width:10%;">￥&nbsp;&nbsp;</span>
              <el-input-number
              ref="costPrice"
								size="small"
								controls-position="right"
								class="newclassroomNumber"
								style="width:45%;"
								v-model="goodData.costPrice"
								:precision="2"
								:step="0.01"
								:min="0"
								:max="9999"
							>
              </el-input-number>

              <!-- <el-input ref="costPrice" style="width:50%" maxlength="9" onkeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))"
                oninput="value=value.replace(/[^\d]/g,'')" v-model="goodData.costPrice" placeholder="请输入成本价">
                <template slot="prepend">￥</template>
              </el-input> -->
            </el-form-item>
            <el-form-item label="售卖单价" :rules="[
      { required: true, message: ''}]">
      <span style="width:10%;">￥&nbsp;&nbsp;</span>
              <el-input-number
              ref="salesPrice"
								size="small"
								controls-position="right"
								class="newclassroomNumber"
								style="width:45%;"
								v-model="goodData.salesPrice"
								:precision="2"
								:step="0.01"
								:min="0"
								:max="9999"
							>
              </el-input-number>
              <!-- <el-input ref="salesPrice" style="width:50%" maxlength="9" onkeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))"
                oninput="value=value.replace(/[^\d]/g,'')" v-model="goodData.salesPrice" placeholder="请输入售卖单价">
                <template slot="prepend">￥</template>
              </el-input> -->
            </el-form-item>
            <el-form-item label="库存数量">
              <el-input ref="stockNum"  :disabled="!isAdd" maxlength="9" style="width:50%" onkeypress="return(/[\d]/.test(String.fromCharCode(event.keyCode)))"
                oninput="value=value.replace(/[^\d]/g,'')" v-model="goodData.stockNum" placeholder="请输入库存数量"></el-input>
            </el-form-item>
          </el-form>

        </div>
        <div class="btn_div">
          <div class="btn_sure" @click="sure()">确 认</div>
          <div class="btn_cancel" @click="cancel()">取 消</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  var vm;
  var user;
  export default {
    data: function() {
      return {
        show: false,
        open: false,
        visible: false,
        isAdd:false,
        title: "",
        goodData: {},
        goodsModeList: []
      };
    },
    methods: {
      init: function(data) {
        vm.show = true;
        setTimeout(function() {
          vm.open = true;
          vm.visible = true;
        }, 11);
        if (data && data.goodsId) {
          vm.isAdd = false;
          vm.title = "编辑商品";
          vm.goodData = data;
        } else {
          vm.isAdd = true;
          vm.title = "新增商品";
          vm.goodData = {
            name: "",
            goodsMode: "",
            costPrice: "",
            salesPrice: "",
            stockNum: "",
            campusId: this.$store.state.campusId
          }
        }

      },
      sure: function() {
        var rules = [];
        let name = this.$refs.name;
        let goodsMode = this.$refs.goodsMode;
        let salesPrice = this.$refs.salesPrice;
        rules.push({
          judge: tool.judge("classname", vm.goodData.name),
          focus: name,
          tips: "请输入2~19位由汉字,字母,数字组成的非空无特殊字符的商品名称"
        });
        rules.push({
          judge: vm.goodData.goodsMode,
          focus: goodsMode,
          tips: "请选择商品类型"
        });
        console.log(vm.goodData.salesPrice);
        rules.push({
          judge: vm.goodData.salesPrice,
          focus: salesPrice,
          tips: "请输入商品售卖单价"
        });
        if (!tool.validate(rules)) {
          return;
        }
        if(vm.isAdd){
        api.goodAdd(vm.goodData).then(function(data) {
          if (data.resultCode == 2000) {
            vm.$message({
              message: "新增成功",
              type: "success"
            });
            vm.$emit("callback", "");
            vm.cancel();
          } else {
            vm.$message.error(data.message);
          }
        });
        }else{
          vm.goodData.type = 2;
          api.goodUpdate(vm.goodData).then(function(data) {
            if (data.resultCode == 2000) {
              vm.$message({
                message: "编辑成功",
                type: "success"
              });
              vm.$emit("callback", "");
              vm.cancel();
            } else {
              vm.$message.error(data.message);
            }
          });
        }
      },
      cancel: function() {
        //隐藏弹窗
        vm.open = false;
        setTimeout(function() {
          vm.show = false;
          vm.visible = false;
          vm.isAdd = false;
        }, 500);
      }
    },
    created: function() {
      vm = this;
      let dict = tool.getItem(lsKey.dict, true);
      user = tool.getItem(lsKey.user, true); //得到当前用户信息
      vm.goodsModeList = dict["goods_mode"];
      console.log(vm.goodsModeList);
    }
  };
</script>
<style lang="scss">
  .newgood {

    .el-form--inline .el-form-item {
      vertical-align: inherit;
    }

    .el-input-group__append,
    .el-input-group__prepend {
      background-color: #ffffff;
      color: #666666;
      border: 0rem solid #ffffff;
      font-size: 0.16rem;
      padding: 0 0.1rem;
    }

    .el-radio {
      margin-right: 0.1rem;
    }

    .el-input__icon {
      line-height: 0.32rem;
    }

    .el-form-item__label {
      font-size: 0.16rem;
      color: #666666;
    }
  }
</style>
<style scoped src="@/style/clue_manage/clue_add.css"></style>
<style scoped src="@/style/module/dialog.css"></style>
<style scoped>
  .panel {
    width: 8rem;
    padding-bottom: 0.2rem;
  }
</style>
